package cn.lichenes.controller;

import cn.lichenes.entity.Indent;
import cn.lichenes.entity.Phone;
import cn.lichenes.entity.User;
import cn.lichenes.log.SystemLog;
import cn.lichenes.service.IndentService;
import cn.lichenes.service.PhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndentController {

    @Autowired
    private IndentService indentService;
    @Autowired
    private PhoneService phoneService;

    @SystemLog(value = "用户添加购物车",status = "user")
    @PostMapping("/indent")
    @ResponseBody
    public boolean addIndent(Integer pid, HttpSession session){
        Map<String, Object> phoneById = phoneService.getPhoneById(pid);
        Phone phone = (Phone) phoneById.get("phone");
        User user = (User) session.getAttribute("user");
        if (user != null && phone.getpResidue() > 0){
            return indentService.add(user,pid);
        }
        return false;
    }

    @GetMapping("/indent")
    public String indent(HttpSession session, @RequestParam(value = "page",defaultValue = "1") Integer page, Model model){
        User user = (User) session.getAttribute("user");
        if (user != null){
            Page<Indent> indentPage = indentService.findAll(user, page);
            Map<String,Object> map = new HashMap<>();
            map.put("indents",indentPage.getContent());
            map.put("currentPage",page);
            map.put("totalPages",indentPage.getTotalPages());
            model.addAllAttributes(map);
            return "indent";

        }
        return "redirect:/login";
    }

    @SystemLog(value = "用户删除购物车商品",status = "user")
    @DeleteMapping("/indent/{id}")
    public String deleteIndent(@PathVariable("id") Integer id){
        System.out.println(id);
        boolean flag = indentService.delete(id);
        return "redirect:/indent";
    }

    @GetMapping("/order")
    public String order(HttpSession session, Integer page,Model model){
        User user = (User) session.getAttribute("user");
        Page<Indent> indentPage = indentService.findByUser(user, page);
        Map<String,Object> map = new HashMap<>();
        map.put("indents",indentPage.getContent());
        map.put("currentPage",1);
        map.put("totalPages",indentPage.getTotalPages());
        model.addAllAttributes(map);
        return "account::order";
    }

    @SystemLog(value = "用户签收订单",status = "user")
    @PutMapping("/order/{id}")
    public String order(@PathVariable("id")Integer id){
        System.out.println(id);
        boolean flag = indentService.sign(id);
        return "redirect:/account";
    }

    @GetMapping("/management-indent")
    public String managementIndent(@RequestParam(value = "page",defaultValue = "1")Integer page,Model model){
        Page<Indent> indentPage = indentService.getAll(page);
        Map<String,Object> map = new HashMap<>();
        map.put("indents",indentPage.getContent());
        map.put("currentPage",page);
        map.put("totalPages",indentPage.getTotalPages());
        model.addAllAttributes(map);
        return "management-indent";
    }

    @SystemLog(value = "管理员发送商品",status = "admin")
    @PutMapping("/management-indent/{id}")
    public String updateIndent(@PathVariable("id")Integer id){
        boolean flag = indentService.updateIndent(id);
        return "redirect:/management-indent";
    }
}
